package com.ruoyi.timedTask;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.index.domain.FhIndexHot;
import com.ruoyi.index.domain.FhIndexPostinfo;
import com.ruoyi.index.service.IFhIndexHotService;
import com.ruoyi.index.service.IFhIndexPostinfoService;
import com.ruoyi.info.domain.FhInfoSigninlist;
import com.ruoyi.info.domain.FhInfoTasklist;
import com.ruoyi.info.domain.FhInfoUser;
import com.ruoyi.info.domain.FhInfoUsertask;
import com.ruoyi.info.service.IFhInfoSigninlistService;
import com.ruoyi.info.service.IFhInfoTasklistService;
import com.ruoyi.info.service.IFhInfoUserService;
import com.ruoyi.info.service.IFhInfoUsertaskService;
import com.ruoyi.map.domain.FhMapFishtype;
import com.ruoyi.map.domain.FhMapWater;
import com.ruoyi.map.domain.FhMapWatercommonfish;
import com.ruoyi.map.service.IFhMapFishtypeService;
import com.ruoyi.map.service.IFhMapWaterService;
import com.ruoyi.map.service.IFhMapWatercommonfishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class TimedTask {

    @Autowired
    private IFhIndexPostinfoService fhIndexPostinfoService;

    @Autowired
    private IFhIndexHotService iFhIndexHotService;

    @Autowired
    private IFhInfoSigninlistService fhInfoSigninlistService;

    @Autowired
    private IFhInfoUserService fhInfoUserService;

    @Autowired
    private IFhInfoTasklistService iFhInfoTasklistService;

    @Autowired
    private IFhInfoUsertaskService iFhInfoUsertaskService;

    @Autowired
    private IFhMapWaterService fhMapWaterService;

    @Autowired
    private IFhMapWatercommonfishService fhMapWatercommonfishService;

    @Autowired
    private IFhMapFishtypeService fhMapFishtypeService;

    //定时更新热点表，每小时更新一次
    @Scheduled(cron = "0 20 0 1/1 * ?")
    public void timedUpdateHot() {
        FhIndexPostinfo fhIndexPostinfo = new FhIndexPostinfo();
        List<FhIndexPostinfo> fhIndexPostinfos = fhIndexPostinfoService.selectFhIndexPostinfoList(fhIndexPostinfo);
        //List接口自身的sort方法对对象集合排序，重写Comparator接口方法即可
        fhIndexPostinfos.sort(new Comparator<FhIndexPostinfo>() {
            @Override
            public int compare(FhIndexPostinfo post1, FhIndexPostinfo post2) {
                Long likes1 = post1.getLikes();
                Long likes2 = post2.getLikes();
                return likes1.compareTo(likes2);
            }
        });
        //清空热点表数据
        FhIndexHot fhIndexHot = new FhIndexHot();
        List<FhIndexHot> fhIndexHots = iFhIndexHotService.selectFhIndexHotList(fhIndexHot);
        Long[] ids = new Long[50];
        for (int i = 0; i < fhIndexHots.size(); i++) {
            ids[i] = fhIndexHots.get(i).getId();
        }
        iFhIndexHotService.deleteFhIndexHotByIds(ids);
        //向热点表插入数据
        int i = fhIndexPostinfos.size();
        if (i > 20) {
            for (int i1 = i - 1; i1 >= i - 20; i1--) {
                if (fhIndexPostinfos.get(i1).getHotWords() == null) {
                    continue;
                }
                fhIndexHot.setPostinfoId(fhIndexPostinfos.get(i1).getId());
                fhIndexHot.setLikeNum(fhIndexPostinfos.get(i1).getLikes());
                fhIndexHot.setHotWords(fhIndexPostinfos.get(i1).getHotWords());
                iFhIndexHotService.insertFhIndexHot(fhIndexHot);
            }
        } else {
            for (int i1 = i - 1; i1 >= 0; i1--) {
                if (fhIndexPostinfos.get(i1).getHotWords() == null) {
                    continue;
                }
                fhIndexHot.setPostinfoId(fhIndexPostinfos.get(i1).getId());
                fhIndexHot.setLikeNum(fhIndexPostinfos.get(i1).getLikes());
                fhIndexHot.setHotWords(fhIndexPostinfos.get(i1).getHotWords());
                iFhIndexHotService.insertFhIndexHot(fhIndexHot);
            }
        }
    }


    //定时更新用户签到表，每天更新一次,更新签到表
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void updateSigninList(){
        //比对这次和上次签到的日期是否相邻，相邻的话连续签到日期加一
        //查询上次的签到日期，和连续签到天数
        FhInfoSigninlist fhInfoSigninlist = new FhInfoSigninlist();
        List<FhInfoSigninlist> fhInfoSigninlists = fhInfoSigninlistService.selectFhInfoSigninlistList(fhInfoSigninlist);
        for (int i = 0; i < fhInfoSigninlists.size(); i++) {
            Calendar aCalendar = Calendar.getInstance();
            aCalendar.setTime(fhInfoSigninlists.get(i).getSinginDate());
            int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
            aCalendar.setTime(new Date());
            int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
            //连续签到
            if((day2-day1)!=1){
                //修改签到表
                fhInfoSigninlists.get(i).setSinginDay(0L);
                fhInfoSigninlistService.updateFhInfoSigninlist(fhInfoSigninlists.get(i));
            }
        }
    }


    //每日更新用户任务表
    @Scheduled(cron = "0 10 0 1/1 * ?")
    public void updateUserTask(){
        FhInfoUser fhInfoUser = new FhInfoUser();
        List<FhInfoUser> fhInfoUserList = fhInfoUserService.selectFhInfoUserList(fhInfoUser);
        for (int i1 = 0; i1 < fhInfoUserList.size(); i1++) {
            Long userId = fhInfoUserList.get(i1).getUserId();

            FhInfoUsertask fhInfoUsertask = new FhInfoUsertask();
            fhInfoUsertask.setUserId(userId);
            List<FhInfoTasklist> fhInfoTasklists = iFhInfoTasklistService.selectFhInfoTasklistList(new FhInfoTasklist());
            List<FhInfoUsertask> list = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask);
            //用户任务列表为空
            if(list.isEmpty()){
                for (int i = 0; i < fhInfoTasklists.size(); i++) {
                    BeanUtils.copyProperties(fhInfoTasklists.get(i),fhInfoUsertask);
                    fhInfoUsertask.setTaskTime(new Date());
                    fhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                    iFhInfoUsertaskService.insertFhInfoUsertask(fhInfoUsertask);
                }
            }else {
                //用户任务列表不为空,先判断任务日期是否为同一天，不是的话则
                // 先删除每日任务，再刷新每日任务
                if(!DateUtils.isSameDay(list.get(0).getTaskTime(),new Date())){
                    for (int i = 0; i < list.size(); i++) {
                        if(list.get(i).getTaskType().equals("0")){
                            iFhInfoUsertaskService.deleteFhInfoUsertaskById(list.get(i).getId());
                        }
                    }
                    for (int i = 0; i < fhInfoTasklists.size(); i++) {
                        //0是每日任务，1是一次性任务
                        if (fhInfoTasklists.get(i).getTaskType().equals("0")){
                            FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                            BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                            newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                            iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                        }else {
                            // 任务类型为一次性任务，如果用户任务表表中有该一次性任务，则不添加
                            Boolean flag = true;
                            for (int j = 0; j < list.size(); j++) {
                                if(fhInfoTasklists.get(i).getTask().equals(list.get(j).getTask()) ){
                                    flag = false;
                                }
                            }
                            if(flag){
                                FhInfoUsertask newFhInfoUsertask = new FhInfoUsertask();
                                BeanUtils.copyProperties(list.get(i),newFhInfoUsertask);
                                newFhInfoUsertask.setTaskId(fhInfoTasklists.get(i).getId());
                                iFhInfoUsertaskService.insertFhInfoUsertask(newFhInfoUsertask);
                            }
                        }
                    }
                }
            }
            FhInfoUsertask fhInfoUsertask1 = new FhInfoUsertask();
            fhInfoUsertask1.setUserId(fhInfoUsertask.getUserId());
            List<FhInfoUsertask> fhInfoUsertasks = iFhInfoUsertaskService.selectFhInfoUsertaskList(fhInfoUsertask1);
        }

    }


    //定时更新水域常见鱼种表
    @Scheduled(cron ="0 30 0 1/1 * ?")
    public void timedUpdateWaterCommonFish() {
        FhMapWater fhMapWater = new FhMapWater();
        List<FhMapWater> fhMapWaters = fhMapWaterService.selectFhMapWaterList(fhMapWater);
        //清空水域常见鱼种表
        Long[] waterIdList = new Long[fhMapWaters.size()];
        for (int i = 0; i < fhMapWaters.size(); i++) {
            waterIdList[i] = fhMapWaters.get(i).getId();
        }
        fhMapWatercommonfishService.deleteFhMapWatercommonfishByWaterIds(waterIdList);
        //更新水域常见鱼种表
        for (int i = 0; i < fhMapWaters.size(); i++) {
            FhMapWater fhMapWaterInfo = fhMapWaters.get(i);
            FhIndexPostinfo fhIndexPostinfo = new FhIndexPostinfo();
            fhIndexPostinfo.setLongitude(fhMapWaterInfo.getLongitude());
            fhIndexPostinfo.setLatitude(fhMapWaterInfo.getLatitude());
            List<FhIndexPostinfo> fhIndexPostinfos = fhIndexPostinfoService.selectFhIndexPostinfoList(fhIndexPostinfo);
            //该水域出现捕获过的鱼种以及次数
            Map<String, Integer> fishIdCatchNum = new HashMap<>();
            for (int j = 0; j < fhIndexPostinfos.size(); j++) {
                FhIndexPostinfo fhIndexPostinfoInfo = fhIndexPostinfos.get(j);
                String[] fishIdList = fhIndexPostinfoInfo.getFishIds().split(",");
                for (String str : fishIdList) {
                    fishIdCatchNum.put(str, fishIdCatchNum.getOrDefault(str, 0) + 1);
                }
            }
            System.out.println(fishIdCatchNum);
            //统计该水域捕获总数，每种渔获种类的数量，更新到数据库
            int waterCatchCount = 0;
            for (Map.Entry<String, Integer> entry : fishIdCatchNum.entrySet()) {
                waterCatchCount+=entry.getValue();
                FhMapWatercommonfish fhMapWatercommonfish = new FhMapWatercommonfish();
                fhMapWatercommonfish.setWaterId(fhMapWaterInfo.getId());
                fhMapWatercommonfish.setWaterName(fhMapWaterInfo.getWaterName());
                fhMapWatercommonfish.setFishId(Long.valueOf(entry.getKey()));
                fhMapWatercommonfish.setCatchNum(Long.valueOf(entry.getValue()));
                FhMapFishtype fhMapFishtype = fhMapFishtypeService.selectFhMapFishtypeById(Long.valueOf(entry.getKey()));
                fhMapWatercommonfish.setFishImage(fhMapFishtype.getFishImage());
                fhMapWatercommonfish.setFishName(fhMapFishtype.getFishType());
                //更新水域常见鱼类表
                fhMapWatercommonfishService.insertFhMapWatercommonfish(fhMapWatercommonfish);
            }
            //更新该水域捕获总数
            fhMapWaterInfo.setCatchTotal(Long.valueOf(waterCatchCount));
            fhMapWaterService.updateFhMapWater(fhMapWaterInfo);
        }
    }

}
